package com.ms.game.common.message;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.ms.game.gmtool.ServerShutdownType;
import com.ms.game.gmtool.message.SGmtoolKickAllConnectionNotify;
import com.ms.game.gmtool.message.SGmtoolServerShutdownCountdownNotify;
import com.ms.game.gmtool.message.SGmtoolServerShutdownCountdownNotifyToClient;
import com.xpec.c4.game.utility.GUID;
import com.xpec.c4.message.MessageTypes;
import com.xpec.c4.service.annotations.MessageType;
import com.xpec.c4.service.common.BaseMessageAction;
import com.xpec.c4.service.group.transportlayer.BroadcastTargetTypes;
import com.xpec.c4.service.group.transportlayer.GroupMessage;
import com.xpec.c4.service.message.MessageAction;
import com.xpec.c4.service.module.BasicServiceModule;

/**
 * 處理 server 維護相關消息
 * 
 * @author TW15010
 */
public class ServerMaintainAction extends BaseMessageAction {
	private static final Log log = LogFactory.getLog(ServerMaintainAction.class);

	@MessageType(typeCode = MessageTypes.S_GMTOOL_SERVER_SHUTDOWN_COUNTDOWN_NOTIFY)
	public void recvServerShutdownCountdownNotify(SGmtoolServerShutdownCountdownNotify notify) {
		BasicServiceModule module = getModule();
		if (module != null) {
			module.onGameRecServerShutdownCountdownNotify(notify);
		}
	}

	@MessageType(typeCode = MessageTypes.S_GMTOOL_KICK_ALL_CONNECTION_NOTIFY)
	public void recvKickAllConnectionNotify(Object object) {
		BasicServiceModule module = getModule();
		if (module != null) {
			SGmtoolKickAllConnectionNotify request = (SGmtoolKickAllConnectionNotify) object;
			module.onGameRecvKickAllConnectionNotify(request);
		}
	}

	@MessageType(typeCode = MessageTypes.S_GMTOOL_STOP_TRANSACTION_NOTIFY)
	public void recvStopTransactionNotify(Object object) {
		BasicServiceModule module = getModule();
		if (module != null) {
			module.onRecvStopTransactionNotify();
		}
	}
	
	@MessageType(typeCode = MessageTypes.S_GMTOOL_MODIFY_SERVER_STATUS_NOTIFY)
	public void recvChangeServerMaintainNotify(Integer level){
		BasicServiceModule module = getModule();
		if (module != null) {
			module.onRecvChangeLevelNotify(level);
		}
	}

	/**
	 * 廣播 server 進入關機倒數狀態的通知給所有或指定 module
	 * 
	 * @param notify
	 */
	public static void broadcastServerShutdownCountdownNotifyToAllModule(SGmtoolServerShutdownCountdownNotify notify, List<String> destModules) {
		if (destModules == null || destModules.size() == 0) {
			notify.setShutdownType(ServerShutdownType.All_Game);
			GroupMessage gmsg = new GroupMessage();
			gmsg.setDestModuleID(null);
			gmsg.setMessageType(MessageTypes.S_GMTOOL_SERVER_SHUTDOWN_COUNTDOWN_NOTIFY);
			gmsg.setMessageContent(notify);
			MessageAction.sendMessage(gmsg);
		} else {
			notify.setShutdownType(ServerShutdownType.Single_Game);
			for (String destModule : destModules) {
				GroupMessage gmsg = new GroupMessage();
				gmsg.setDestModuleID(destModule);
				gmsg.setMessageType(MessageTypes.S_GMTOOL_SERVER_SHUTDOWN_COUNTDOWN_NOTIFY);
				gmsg.setMessageContent(notify);
				MessageAction.sendMessage(gmsg);
			}
		}
	}

	/**
	 * 廣播 server 進入踢玩家斷線狀態給所有或指定 module
	 */
	public static void broadcastKickAllConnectionNotify(List<String> destModules) {
		
		SGmtoolKickAllConnectionNotify notify = new SGmtoolKickAllConnectionNotify();
		if(destModules == null || destModules.size() == 0){
			notify.setShutdownType(ServerShutdownType.All_Game);
			GroupMessage gmsg = new GroupMessage();
			gmsg.setDestModuleID(null);
			gmsg.setMessageContent(notify);
			gmsg.setMessageType(MessageTypes.S_GMTOOL_KICK_ALL_CONNECTION_NOTIFY);
			MessageAction.sendMessage(gmsg);
		}
		else{
			notify.setShutdownType(ServerShutdownType.Single_Game);
			for(String destModule : destModules){
				GroupMessage gmsg = new GroupMessage();
				gmsg.setDestModuleID(destModule);
				gmsg.setMessageContent(notify);
				gmsg.setMessageType(MessageTypes.S_GMTOOL_KICK_ALL_CONNECTION_NOTIFY);
				MessageAction.sendMessage(gmsg);
			}
		}
	}

	/**
	 * 廣播 server 進入server 進入禁止交易行為狀態給所有或指定 module
	 */
	public static void broadcastStopTransactionNotify(List<String> destModules) {
		if(destModules == null || destModules.size() == 0){
			GroupMessage gmsg = new GroupMessage();
			gmsg.setDestModuleID(null);
			gmsg.setMessageType(MessageTypes.S_GMTOOL_STOP_TRANSACTION_NOTIFY);
			MessageAction.sendMessage(gmsg);
		}
		else{
			for(String destModule : destModules){
				GroupMessage gmsg = new GroupMessage();
				gmsg.setDestModuleID(destModule);
				gmsg.setMessageType(MessageTypes.S_GMTOOL_STOP_TRANSACTION_NOTIFY);
				MessageAction.sendMessage(gmsg);
			}
		}
		
	}

	/**
	 * 廣播關機倒數通知給 client 
	 * 2017-08-14 
	 * 全遊戲維護由GateWay廣播給Client，單一遊戲維護，跟著 {@link #broadcastServerShutdownCountdownNotifyToAllModule} 送到各遊戲，由各遊戲&DM各自負責
	 * @param countdownTime
	 *            倒數時間 (ms)
	 */
	public static void broadcastServerShutdownCountdownNotifyToClient(long countdownTime, String[] gameTypes) {
		SGmtoolServerShutdownCountdownNotifyToClient notify = new SGmtoolServerShutdownCountdownNotifyToClient();
		notify.setCountdownTime(countdownTime);

		if (gameTypes == null) { //伺服器維護
			notify.setCountdownType(ServerShutdownType.All_Game.getId());
			MessageAction.broadCastMessageIncludeRobot(MessageTypes.S_GMTOOL_SERVER_SHUTDOWN_COUNTDOWN_NOTIFY_TO_CLIENT, notify, BroadcastTargetTypes.BROADCAST_TO_ALL);
		}
	}
	
	/**
	 * 廣播 server  把指定的狀態給所有或指定 module
	 */
	public static void broadcastModifyServerStatusNotify(Integer level, List<String> destModules) {
		if(destModules == null || destModules.size() == 0){
			GroupMessage gmsg = new GroupMessage();
			gmsg.setMessageContent(level);
			gmsg.setDestModuleID(null);
			gmsg.setMessageType(MessageTypes.S_GMTOOL_MODIFY_SERVER_STATUS_NOTIFY);
			MessageAction.sendMessage(gmsg);
		}
		else{
			for(String destModule : destModules){
				GroupMessage gmsg = new GroupMessage();
				gmsg.setMessageContent(level);
				gmsg.setDestModuleID(destModule);
				gmsg.setMessageType(MessageTypes.S_GMTOOL_MODIFY_SERVER_STATUS_NOTIFY);
				MessageAction.sendMessage(gmsg);
			}
		}
	}

	/**
	 * gmtool 廣播給所有 module, 清除 zone 上 LoginPlayerList 內指定的 guid
	 * 
	 * @param playerGuid
	 */
	public static void broadcastRemoveGuidFromZoneLoginPlayerList(GUID playerGuid) {
		GroupMessage gmsg = new GroupMessage();
		gmsg.setMessageContent(playerGuid);
		gmsg.setDestModuleID(null);
		gmsg.setMessageType(MessageTypes.S_GMTOOL_REMOVE_GUID_FROM_ZONE_LOGIN_PLAYER_LIST);
		MessageAction.sendMessage(gmsg);
	}
}
